/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.supplier.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.supplier.entity.Supplier;
import com.pitop.supplier.entity.SupplierState;
import com.pitop.supplier.service.SupplierSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 供应商 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/supplier")
@Slf4j
@Api(value = "供应商控制器", description = "供应商控制器")
public class SupplierCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private SupplierSVImpl supplierSVImpl;


    /**
     * 查询Supplier一个详情信息
     *
     * @param id   id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Supplier一个详情信息", notes = "查询Supplier一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Supplier supplier = supplierSVImpl.load(id, code);
        log.info(JSON.toJSONString(supplier));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, supplier);
    }


    /**
     * 根据条件id查询Supplier一个详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Supplier一个详情信息", notes = "查询Supplier一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Supplier supplier = supplierSVImpl.loadById(id);
        log.info(JSON.toJSONString(supplier));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, supplier);
    }

    /**
     * 根据条件code查询Supplier一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Supplier一个详情信息", notes = "查询Supplier一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Supplier supplier = supplierSVImpl.loadByCode(code);
        log.info(JSON.toJSONString(supplier));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, supplier);
    }


    /**
     * 查询Supplier信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Supplier信息集合", notes = "查询Supplier信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "sortColumns", value = "排序：code DESC, code ASE", paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Supplier supplier, @ApiIgnore Page<Supplier> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Supplier> suppliers = supplierSVImpl.list(supplier, page.getCurPage(), page.getPageSize());
        int total = supplierSVImpl.count(supplier);
        page.setTotalRow(total);
        page.setVoList(suppliers);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "查询成功", page);
    }


    /**
     * 查询Supplier信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Supplier信息集合", notes = "查询Supplier信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, @ApiIgnore Page<Supplier> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Supplier> suppliers = supplierSVImpl.list(id, code, page.getCurPage(), page.getPageSize());
        int total = supplierSVImpl.count(id, code);
        page.setTotalRow(total);
        page.setVoList(suppliers);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "查询成功", page);
    }


    /**
     * 查询Supplier信息集合
     *
     * @return List<Supplier>
     */
    @ApiOperation(value = "查询Supplier信息集合", notes = "查询Supplier信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialCode", value = "编码", paramType = "query", required = true)
    })
    @GetMapping(value = "/loadListByMaterialCode")
    @ResponseBody
    public BeanRet loadListByMaterialCode(String materialCode) {
        List<Supplier> suppliers = supplierSVImpl.loadListByMaterialCode(materialCode);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "查询成功", suppliers);
    }

    /**
     * 创建Supplier
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Supplier", notes = "创建Supplier")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "供应商名称 最长32个汉字 64个英文", paramType = "query"),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query"),
            @ApiImplicitParam(name = "contacts", value = "联系人", paramType = "query"),
            @ApiImplicitParam(name = "bank", value = "开户行(最长32个汉字 64个英文)", paramType = "query"),
            @ApiImplicitParam(name = "bankAccount", value = "开户行账户", paramType = "query"),
            @ApiImplicitParam(name = "paymentTerm", value = "支付方式:通过调用'支付方式'接口查询", paramType = "query"),
            @ApiImplicitParam(name = "creditRating", value = "信用等级", paramType = "query"),
            @ApiImplicitParam(name = "supplierCategoryCode", value = "供应商类型编码", paramType = "query"),
            @ApiImplicitParam(name = "website", value = "公司官网", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址 64个汉字，128个英文字符", paramType = "query"),
            @ApiImplicitParam(name = "englishAddress", value = "英文地址", paramType = "query"),
            @ApiImplicitParam(name = "email", value = "电子邮箱", paramType = "query"),
            @ApiImplicitParam(name = "fax", value = "传真号", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "移动电话", paramType = "query"),
            @ApiImplicitParam(name = "telephone", value = "固定电话", paramType = "query"),
            @ApiImplicitParam(name = "zipCode", value = "邮编", paramType = "query"),
            @ApiImplicitParam(name = "companyNature", value = "公司性质(1011) 国营 Staterun ,民营Privately_Operated，合资Joint_Venture，外资 Foreign_Capital", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1010)：启用 Enable,禁用 Disable ", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "备注 128 个汉字说明", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore Supplier supplier) {
        if (StringUtils.isEmpty(supplier.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getContacts())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getBank())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getBankAccount())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getSupplierCategoryCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getAddress())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getEmail())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getMobile())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getCompanyNature())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getSummary())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        supplierSVImpl.save(supplier);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, supplier);
    }


    /**
     * 修改供应商
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改供应商", notes = "修改供应商")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "供应商名称 最长32个汉字 64个英文", paramType = "query"),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query"),
            @ApiImplicitParam(name = "contacts", value = "联系人", paramType = "query"),
            @ApiImplicitParam(name = "bank", value = "开户行(最长32个汉字 64个英文)", paramType = "query"),
            @ApiImplicitParam(name = "bankAccount", value = "开户行账户", paramType = "query"),
            @ApiImplicitParam(name = "paymentTerm", value = "支付方式:通过调用'支付方式'接口查询", paramType = "query"),
            @ApiImplicitParam(name = "creditRating", value = "信用等级", paramType = "query"),
            @ApiImplicitParam(name = "supplierCategoryCode", value = "供应商类型编码", paramType = "query"),
            @ApiImplicitParam(name = "website", value = "公司官网", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址 64个汉字，128个英文字符", paramType = "query"),
            @ApiImplicitParam(name = "englishAddress", value = "英文地址", paramType = "query"),
            @ApiImplicitParam(name = "email", value = "电子邮箱", paramType = "query"),
            @ApiImplicitParam(name = "fax", value = "传真号", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "移动电话", paramType = "query"),
            @ApiImplicitParam(name = "telephone", value = "固定电话", paramType = "query"),
            @ApiImplicitParam(name = "zipCode", value = "邮编", paramType = "query"),
            @ApiImplicitParam(name = "companyNature", value = "公司性质(1011) 国营 Staterun ,民营Privately_Operated，合资Joint_Venture，外资 Foreign_Capital", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1010)：启用 Enable,禁用 Disable ", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "备注 128 个汉字说明", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Supplier supplier) {
        if (StringUtils.isEmpty(supplier.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getContacts())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getBank())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getBankAccount())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getSupplierCategoryCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getAddress())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getEmail())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getMobile())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getCompanyNature())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(supplier.getSummary())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        supplierSVImpl.modify(supplier);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, supplier);
    }

    /**
     * 修改供应商状态
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改供应商状态", notes = "修改供应商状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "状态(1010)：启用 Enable,禁用 Disable ", paramType = "query", required = true),
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, SupplierState state) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (state == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        supplierSVImpl.updateState(code, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }


    /**
     * 根据主键删除Supplier
     *
     * @return BeanRet
     */
  /*  @ApiOperation(value = "删除Supplier", notes = "删除Supplier")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        if (id != null && StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        supplierSVImpl.delete(id, code);
        return BeanRet.create(true, "删除Supplier成功");
    }*/


}
